package com.jingxi.sso.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jingxi.common.pojo.JingXiResult;
import com.jingxi.model.TbUser;
import com.jingxi.sso.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController {

	@Autowired
	private UserService userService;

	@RequestMapping("/check/{param}/{type}")
	@ResponseBody
	public Object checkData(@PathVariable String param, @PathVariable Integer type, String callback) {

		JingXiResult result = null;

		// 检验参数的有效性
		if (StringUtils.isBlank(param)) {
			result = JingXiResult.build(400, "检验内容不能为空");
		}
		if (type == null) {
			result = JingXiResult.build(400, "检验内容不能为空");
		}
		if (type != 1 && type != 2 && type != 3) {
			result = JingXiResult.build(400, "检验内容类型出错");
		}

		if (null != result) {
			if (null != callback) {
				MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(result);
				mappingJacksonValue.setJsonpFunction(callback);
				return mappingJacksonValue;
			} else {
				return result;
			}
		}
		// 调用服务

		try {
			result = userService.checkData(param, type);
		} catch (Exception e) {
			result = JingXiResult.build(500, e.getMessage());
		}
		if (null != callback) {
			MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(result);
			mappingJacksonValue.setJsonpFunction(callback);
			return mappingJacksonValue;
		} else {
			return result;
		}
	}

	@RequestMapping(value = "/register", method = RequestMethod.POST)
	@ResponseBody
	public JingXiResult createUser(TbUser user) {
		try {
			JingXiResult result = userService.createUser(user);
			return result;
		} catch (Exception e) {
			return JingXiResult.build(500, e.getMessage());
		}
	}

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	@ResponseBody
	public JingXiResult userLogin(String username, String password, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			JingXiResult result = userService.userLogin(username, password, request, response);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return JingXiResult.build(500, e.getMessage());
		}
	}

	@RequestMapping("/token/{token}")
	@ResponseBody
	public Object getUserInfo(@PathVariable String token, String callback) {
		JingXiResult result = null;
		result = userService.getUserByToken(token);
		System.out.println("Result" + token);
		System.out.println("callback" + callback);
		if (result != null) {
			if (callback != null) {
				MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(result);
				mappingJacksonValue.setJsonpFunction(callback);
				return mappingJacksonValue;
			} else {
				return result;
			}
		}else {
			result = JingXiResult.build(500, "用户不存在");
		}
		return result;
	}

	@RequestMapping("/logout/{token}")
	public Object logout(@PathVariable String token, String callback) {
		JingXiResult result = userService.quitWithSafety(token);

		// jsonp回调方法
		if (callback != null) {
			MappingJacksonValue mValue = new MappingJacksonValue(result);
			mValue.setJsonpFunction(callback);
			return mValue;
		}

		return "redirect:/page/showLogin";
	}

}
